//
//  ZSWaterFlowLayout.m
//  PuBuLiu
//
//  Created by @Zs on 15/12/26.
//  Copyright © 2015年 @Zs. All rights reserved.
//

#import "ZSWaterFlowLayout.h"

/**  每一行的最大列数 */
static const int ZSDelaultMaxColumns = 3;
/**  每一行的间距 */
static const CGFloat ZSDelaultRowMargin = 10;
/**  每一列的间距 */
static const CGFloat ZSDelaultColumnMargin = 10;
/**  上下左右的间距 */
static const UIEdgeInsets ZSDelaultInsets = {10,10,10,10};



@interface ZSWaterFlowLayout()
@property (nonatomic , strong) NSMutableArray *maxYs;//存放每一列最大Y值
@property (nonatomic , strong) NSMutableArray *attrsArray;
@end

@implementation ZSWaterFlowLayout

-(NSMutableArray *)attrsArray{
    
    if (!_attrsArray) {
        self.attrsArray = [[NSMutableArray alloc]init];
    }
    return _attrsArray;
}

-(NSMutableArray *)maxYs{
    
    if (!_maxYs) {
        self.maxYs = [[NSMutableArray alloc]init];
    }
    return _maxYs;
}

-(void)prepareLayout{

    //初始化最大Y值
    [self.maxYs removeAllObjects];
    for (NSUInteger i = 0; i<self.maxColums; i++) {
        [self.maxYs addObject:@(self.insets.top)];
    }
    
    //计算所有cell的布局属性
    [self.attrsArray removeAllObjects];
    NSUInteger count = [self.collectionView numberOfItemsInSection:0];
    for (NSUInteger i = 0; i<count; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];
        
        [self.attrsArray addObject:attrs];
    }
}

/**
 *  如果返回yes,当显示的范围发生改变时就会重新刷新
 *
 *  @param newBounds <#newBounds description#>
 *
 *  @return <#return value description#>
 */
-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{

    return YES;
}


-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
    


    //每一列之间的间距
    CGFloat columnMargin = [self columnMargin];
    //每一列之间的间距
    CGFloat rowMargin = [self rowMargin];
    
    //collectionView的尺寸
    CGFloat collectionW = self.collectionView.bounds.size.width;
    
    UIEdgeInsets insets = self.insets;
    
    int maxColumns = [self maxColums];
    
    //item(cell)的宽度
    CGFloat itemW = (collectionW - insets.left - insets.right - (maxColumns  - 1) * columnMargin) / maxColumns;
    //通过代理,获取indexPath这个位置item的高度
    CGFloat itemH = [self.delegate waterflowLayout:self heightForItemAtIndexPath:indexPath itemWidth:itemW];
    
    //创建属性
    UICollectionViewLayoutAttributes *attrs =  [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    //设置位置和尺寸
    CGFloat minMaxY = [self.maxYs[0]doubleValue];//最短那一列的最大Y值
    int minColumn = 0;//最短那一列的列号
    for (int i = 1; i<maxColumns; i++) {
        CGFloat maxY = [self.maxYs[i]doubleValue];
        if (maxY < minMaxY) {
            minMaxY = maxY;
            minColumn = i;
        }
    }

    
    CGFloat itemX = insets.left + minColumn * (itemW + columnMargin);
    CGFloat itemY = minMaxY + rowMargin;
    //设置尺寸
    attrs.frame = CGRectMake(itemX,itemY, itemW, itemH);
    
    //累加这一列的最大Y值
    self.maxYs[minColumn] = @(CGRectGetMaxY(attrs.frame));
    
    return attrs;
}

-(CGSize)collectionViewContentSize{
    
    CGFloat longMaxY = 0;
    if (self.maxYs.count) {
        longMaxY = [self.maxYs[0]doubleValue];//最短那一列的最大Y值
        for (int i = 1; i<self.maxColums; i++) {
            CGFloat maxY = [self.maxYs[i]doubleValue];
            if (maxY > longMaxY) {
                longMaxY = maxY;
            }
        }
        //累加底部间距
        longMaxY += self.insets.bottom;
    }
    return CGSizeMake(0, longMaxY);
}

/**
 *  一旦位置发生变化就会调用
 *
 *  @param rect <#rect description#>
 *
 *  @return <#return value description#>
 */
-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{

 
    return self.attrsArray;
}

#pragma mark -私有方法(获得代理返回的数字)
-(int)maxColums{
    if ([self.delegate respondsToSelector:@selector(maxColumnsInsetsInWaterflowLayout:)]) {
        return [self.delegate maxColumnsInsetsInWaterflowLayout:self];
    }
    
    return ZSDelaultMaxColumns;
}

-(CGFloat)rowMargin{
    if ([self.delegate respondsToSelector:@selector(rowMarginInsetsInWaterflowLayout:)]) {
        return [self.delegate rowMarginInsetsInWaterflowLayout:self];
    }
    
    return ZSDelaultRowMargin;
}

-(CGFloat)columnMargin{
    if ([self.delegate respondsToSelector:@selector(columnMarginInsetsInWaterflowLayout:)]) {
        return [self.delegate columnMarginInsetsInWaterflowLayout:self];
    }
    
    return ZSDelaultColumnMargin;
}

-(UIEdgeInsets)insets{
    if ([self.delegate respondsToSelector:@selector(insetsInWaterflowLayout:)]) {
        return [self.delegate insetsInWaterflowLayout:self];
    }
    
    return ZSDelaultInsets;
}
@end
